/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telsoft.cms_admin.util;


import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author TUTL
 */
public class BaseController implements Serializable{

    public static final int NEW_ACTION = 1;
    public static final int OLD_ACTION = 0;
    public static final int ACTION_ADD = 2;
    public static final int ACTION_MODIFY = 3;
    public static final int ACTION_REMOVE = 4;
    public static final int ACTION_SEARCH = 5;
    public static final int ACTION_SAVE = 6;
    public static final int ACTION_CANCEL = 7;
    public static final int ACTION_ADDCOPY = 8;
    public boolean bRendered = true;
    public boolean bDisableAddSearch = false;
    public boolean bDisableModifyRemove = true;
    public boolean bCancel = false;
    public boolean bSave = false;
    public boolean bFieldEnable = true;
    public int iAction;

    public boolean isbDisableAddSearch() {
        return bDisableAddSearch;
    }

    public void setbDisableAddSearch(boolean bDisableAddSearch) {
        this.bDisableAddSearch = bDisableAddSearch;
    }

    public boolean isbDisableModifyRemove() {
        return bDisableModifyRemove;
    }

    public void setbDisableModifyRemove(boolean bDisableModifyRemove) {
        this.bDisableModifyRemove = bDisableModifyRemove;
    }

    public boolean isbRendered() {
        return bRendered;
    }

    public boolean isbCancel() {
        return bCancel;
    }

    public boolean isbSave() {
        return bSave;
    }

    public boolean isbFieldEnable() {
        return bFieldEnable;
    }

    public void addErrorMessage(String message) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
    }

    public void addErrorMessage(Exception ex) {
        FacesContext.getCurrentInstance().addMessage(
                null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(),
                null));
    }

    public void addSuccessMessage(String message) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
    }

    public String getParameter(String key, String mstrNull) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map param = facesContext.getExternalContext().getRequestParameterMap();
        return Util.nvl(param.get(key), mstrNull);
    }

    public FacesContext getCurentFacesContext() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext;
    }

    public void redirect(String URL) {
        try {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            String contextPath = facesContext.getExternalContext()
                    .getRequestContextPath();
            facesContext.getExternalContext().redirect(contextPath + URL);
        } catch (IOException ex) {
            Logger.getLogger(BaseController.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
    }

    public String getContextPath() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext.getExternalContext().getRequestContextPath();
    }
}
